package com.laikan.reader.core.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;

/**
 * ͨ通用标题栏
 */
public class TitleLayout extends RelativeLayout {

    private String titleName;
    private TextView tvTitleName;
    private int textColor;
    private int textSize;
    private Button btnBack;
    private Button btnFunc;
    /**
     * 返回图标（左上角）对应的资源文件，如果不设置，则不显示
     */
    private int backIconRes;
    /**
     * 功能图片（右上角）对应的资源文件，如果不设置，则不显示
     */
    private int funcIconRes;
    private boolean isShowBack;
    private boolean isShowFunc;
    private int iconWidth;
    private int iconHeight;
    private TitleBackFunc mTitleBackFunc;

    public TitleLayout(Context context) {
        super(context);
    }

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);
        this.titleName = a.getString(R.styleable.TitleLayout_Title_name);
        this.backIconRes = a.getResourceId(R.styleable.TitleLayout_Title_back_icon, 0);
        this.funcIconRes = a.getResourceId(R.styleable.TitleLayout_Title_func_icon, 0);
        this.isShowBack = a.getBoolean(R.styleable.TitleLayout_Title_is_show_back, true);
        this.isShowFunc = a.getBoolean(R.styleable.TitleLayout_Title_is_show_func, true);

        this.textColor = a.getColor(R.styleable.TitleLayout_Title_text_color, 0);
        this.textSize = a.getDimensionPixelSize(R.styleable.TitleLayout_Title_text_size, 15);

        this.iconWidth = (int)a.getDimension(R.styleable.TitleLayout_Title_icon_width, 10);
        this.iconHeight = (int)a.getDimension(R.styleable.TitleLayout_Title_icon_height, 10);
        // tv.invalidate();
        a.recycle();

        if (0 == backIconRes) {
            isShowBack = false;
        }
        if (0 == funcIconRes) {
            isShowFunc = false;
        }

        // 标题设置
        if (null != titleName) {
            tvTitleName = new TextView(context);
            RelativeLayout.LayoutParams rlpTitleName = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            rlpTitleName.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            tvTitleName.setGravity(Gravity.CENTER);
            tvTitleName.setLayoutParams(rlpTitleName);
            tvTitleName.setText(titleName);
            tvTitleName.setTextSize(px2dip(context, textSize));
            tvTitleName.setTextColor(textColor);

            this.addView(tvTitleName);
        }

        // 返回按钮设置
        if (isShowBack) {
            RelativeLayout.LayoutParams rlpBack = new RelativeLayout.LayoutParams(this.iconWidth, this.iconHeight);
            btnBack = new Button(context);
            rlpBack.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            rlpBack.setMargins(20, 0, 0, 0);
            btnBack.setLayoutParams(rlpBack);
            btnBack.setBackgroundResource(backIconRes);

            this.addView(btnBack);
        }

        // 功能按钮设置
        if (isShowFunc) {
            RelativeLayout.LayoutParams rlpFunc = new RelativeLayout.LayoutParams(this.iconWidth, this.iconHeight);
            btnFunc = new Button(context);
            rlpFunc.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            rlpFunc.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            rlpFunc.setMargins(0, 0, 20, 0);
            btnFunc.setLayoutParams(rlpFunc);
            btnFunc.setBackgroundResource(funcIconRes);

            this.addView(btnFunc);
        }

    }

    public TitleLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }

    public void setBackAndFunc(TitleBackFunc titleBackFunc) {
        this.mTitleBackFunc = titleBackFunc;
        if (null != mTitleBackFunc) {
            if (null != btnBack) {
                btnBack.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mTitleBackFunc.onBack();
                    }
                });
            }
            if (null != btnFunc) {
                btnFunc.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mTitleBackFunc.onFunc();
                    }
                });
            }
        }
    }

    public void setFuncIconRes(int resId) {
        btnFunc.setBackgroundResource(resId);
    }

    public boolean isFuncShow() {
        return btnFunc.isShown();
    }

    /**
     * 设置标题栏功能按钮的隐藏与现实
     *
     * @param isShow true，显示。false，隐藏
     */
    public void setFuncShow(boolean isShow) {
        if (isShow) {
            btnFunc.setVisibility(VISIBLE);
        }else {
            btnFunc.setVisibility(GONE);
        }
    }

    public void setTitleName(String name) {
        tvTitleName.setText(name);
    }

    public interface TitleBackFunc {

        void onBack();

        void onFunc();
    }
}
